// pages/add/manage/manage.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    phone: '',
    idCard: '',
    area: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.registeredInfo();

  },
  delect(e){
    let this_ = this;
    wx.showModal({
      title: '提示',
      content: '是否确认删除当前就诊信息？',
      success (res) {
        //确定
        if (res.confirm) {
          console.log('用户点击确定')
          this_.modifyInfo();
          //取消
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  //用户登录后查询用户就诊信息
  registeredInfo(){
    let this_ = this;
    //从缓存中获得用户信息
    let openId = wx.getStorageSync('openId');
    let s_userName = wx.getStorageSync('userName2');
    let s_phone = wx.getStorageSync('phone2');
    let s_idCard = wx.getStorageSync('idCard2');
    let s_area = wx.getStorageSync('area2');
    if(openId != "" && openId != null){
      console.log(s_userName,s_phone,s_idCard,s_area);
      if(s_userName != "" && s_phone != "" && s_idCard != "" && s_area != ""){
        this_.setData({
          userName: s_userName,
          phone: s_phone,
          idCard: s_idCard,
          area: s_area
        });
      }else{
        wx.request({
          url: 'http://localhost:8060/user/getUser',
          method: 'POST',
          header: { "content-type": "application/x-www-form-urlencoded" },
          data: { 
            openId
          },
          success(responseData){
            console.info("数据库查出的数据：",responseData.data.userInfo.username,responseData.data.userInfo.phone,responseData.data.userInfo.idcard,responseData.data.userInfo.area)
            this_.setData({
              userName: responseData.data.userInfo.username,
              phone: responseData.data.userInfo.phone,
              idCard: responseData.data.userInfo.idcard,
              area: responseData.data.userInfo.area
            });
            //将数据放在缓存中，避免频繁操作服务器
            wx.setStorageSync('userName2', this_.data.userName);
            wx.setStorageSync('phone2', this_.data.phone);
            wx.setStorageSync('id2', responseData.data.userInfo.id);
            wx.setStorageSync('idCard2', this_.data.idCard);
            wx.setStorageSync('area2', this_.data.area);
            console.info("缓存中的数据：",this_.data.userName,this_.data.phone,this_.data.idCard,this_.data.area)
          }
        })
      }
    }

  },
  
  //删除(修改)用户就诊信息
  //删除后显示加载过程，然后返回到添加页面
  modifyInfo(){
    //从缓存中获取openid
    let openid = wx.getStorageSync('openId');
    let userName = "";
    let phone = "";
    let idCard = "";
    let area = "";
    let status = 0;
    console.log(openid);
    if(openid != null || openid != ""){
      wx.request({
        url: 'http://localhost:8060/user/amend',
        method: 'POST',
        header: { "content-type": "application/x-www-form-urlencoded" },
        data: {
          openid,
          userName,
          phone,
          idCard,
          area,
          status
        },
        success(responseData){
          let result = responseData.data.updateStatus;
          console.info(result)
          if(result == 1){
            wx.showToast({
              title: '删除成功',
              icon: 'success'
            });
            //将缓存中的就诊信息置为空
            wx.setStorageSync('userName2', "");
            wx.setStorageSync('phone2', "");
            wx.setStorageSync('idCard2', "");
            wx.setStorageSync('area2', "");
            wx.setStorageSync('id2', "");
            //跳转到添加页面
            wx.switchTab({
              url: '/pages/my/my',
            });
          }
        }
      });
    }  
  }

})